//
//  ActionSheetPickerView.m
//  YoucApp
//
//  Created by Liam on 2/21/13.
//
//

#import "ActionSheetPickerView.h"
#import <QuartzCore/QuartzCore.h>
#import "Util.h"

#define kDuration 0.3

Class g_currentClass = NULL;

@interface ActionSheetPickerView ()

@property (nonatomic, retain) UIView *maskView;

@end

@implementation ActionSheetPickerView

@synthesize maskView;
@synthesize picker;

+ (id)alloc
{
	if (g_currentClass) {
		id object = [g_currentClass allocWithZone:NSDefaultMallocZone()];
		g_currentClass = nil;
		return object;
	}
	return [super alloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)init
{
	g_currentClass = [self class];
	
	NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"ActionSheetPickerView"
                                                          owner:nil
                                                        options:nil];
    
    if ([arrayOfViews count] < 1){
        [self release];
        return nil;
    }
    
    ActionSheetPickerView *newView = [[arrayOfViews objectAtIndex:0] retain];
    
    [self release];
    self = newView;
	
    if (self)
	{
        
    }
    return self;
}

- (void)dealloc
{
	SAFE_RELEASE(maskView);
	
	[super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)showInView:(UIView *) view
{
    CATransition *animation = [CATransition  animation];
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromTop;
    [self setAlpha:1.0f];
    [self.layer addAnimation:animation forKey:nil];
	
	self.layer.shadowOffset = CGSizeMake(0, 0);
    self.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.layer.shadowOpacity = 0.8;
	
	UIView *mainView = [Util rootViewController].view;
    
    self.frame = CGRectMake(0, mainView.frame.size.height - self.frame.size.height, self.frame.size.width, self.frame.size.height);
	
	self.maskView = [[[UIView alloc] initWithFrame:mainView.bounds] autorelease];
	self.maskView.backgroundColor = [UIColor blackColor];
	self.maskView.alpha = 0.2;
	
	animation = [CATransition animation];
	animation.duration = kDuration;
	animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionReveal;
	[self.maskView.layer addAnimation:animation forKey:nil];
	
	[mainView addSubview:self.maskView];
	
    [mainView addSubview:self];
}

#pragma mark - PickerView lifecycle

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return 0;
}

#pragma mark - Button lifecycle

- (void)viewsRemoveFromSuperview
{
	[self removeFromSuperview];
	[self.maskView removeFromSuperview];
}

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{
	if (animated)
	{
		CATransition *animation = [CATransition  animation];
		animation.duration = kDuration;
		animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
		animation.type = kCATransitionPush;
		animation.subtype = kCATransitionFromBottom;
		[self setAlpha:0.0f];
		[self.layer addAnimation:animation forKey:nil];
		
		animation = [CATransition animation];
		animation.duration = kDuration;
		animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
		animation.type = kCATransitionFade;
		[self.maskView.layer addAnimation:animation forKey:nil];
		
		[self performSelector:@selector(viewsRemoveFromSuperview) withObject:nil afterDelay:kDuration];
	}
	else
	{
		[self viewsRemoveFromSuperview];
	}
	
	if (self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)])
	{
        [self.delegate actionSheet:self clickedButtonAtIndex:buttonIndex];
    }
}

- (IBAction)cancel:(id)sender {
    [self dismissWithClickedButtonIndex:0 animated:YES];
}

- (IBAction)locate:(id)sender {
    [self dismissWithClickedButtonIndex:1 animated:YES];
}

@end
